package sort;
/**
 *插入排序 最差时间复杂度O(N^2)
 * @return
 * @author sjm
 * @creed: Wellerman
 * @date 2022/3/3 14:40
 */
public class InsertSort {

    public static void insert(int[] arrs){
        //遍历
        for (int i = 1; i < arrs.length; i++) {
            //判断
            for (int j = i - 1; j>=0 && arrs[j] > arrs[j+1];j--){
                //交换
                swap(arrs,j,j+1);
            }
        }
        for (int arr : arrs) {
            System.out.println(arr);
        }
    }

    private static void swap(int[] arrs, int i, int j) {
        int temp = arrs[i];
        arrs[i] = arrs[j];
        arrs[j] = temp;
    }

    public static void main(String[] args) {
        int[] arrs = {8, 5, 23, 1, 5};
        insert(arrs);
    }
}
